package com.youlai.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.AnswersMapper;
import com.youlai.boot.system.model.qb.Answers;
import com.youlai.boot.system.service.AnswersService;
import lombok.val;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnswersServiceImpl extends ServiceImpl<AnswersMapper, Answers> implements AnswersService {
    @Autowired
    private AnswersMapper answersMapper;
    private static final Logger logger = LoggerFactory.getLogger(AnswersServiceImpl.class);
    @Override
    public boolean saveAnswers(List<Answers> answers) {
        try {
            for (Answers  answers1: answers ){
                QueryWrapper<Answers> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("question",answers1.getQuestion());
                Answers answers2 = answersMapper.selectOne(queryWrapper);
                if (answers2 !=null){
                    answers2.setAnswer(answers1.getAnswer());
                    answers2.setStatus(answers1.getStatus());
                    answers2.setTotal(answers1.getTotal());
                    answersMapper.updateById(answers2);
                }else {
                    answersMapper.insert(answers1);
                }
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
                return false;
        }
    }

    @Override
    public List<Answers> getAllAnswers() {
        return answersMapper.selectList(null);
    }
}
